#include "System.h"

using namespace std;

//添加道路页面
void System::addPath()
{
    setbkcolor(RGB(255, 255, 255));
    cleardevice();
    //顶栏设置
    setfillcolor(RGB(130, 226, 255));
    fillroundrect(0, 0, 375, 83, 0, 0);
    settextcolor(RGB(0, 72, 125));
    settextstyle(36, 0, "黑体");//设置文本大小，垂直压缩，字体
    setbkmode(TRANSPARENT);//去除文字背景
    outtextxy(113, 20, "增加道路");
    IMAGE retn;
    loadimage(&retn, "./返回.png", 16, 29);
    putimage(25, 27, &retn);

    //按钮设置
    BasicButton btn1(17, 716, 152, 64, 10, "添加");
    btn1.setbtncl(47, 205, 14);
    btn1.settext(255, 255, 255, 32, 60, 730);
    btn1.show();

    BasicButton btn2(206, 716, 152, 64, 10, "返回");
    btn2.setbtncl(217, 217, 217);
    btn2.settext(101, 101, 101, 32, 249, 730);
    btn2.show();

    //输入框设置
    settextcolor(RGB(0, 0, 0));
    settextstyle(32, 0, "黑体");//设置文本大小，垂直压缩，字体
    setbkmode(TRANSPARENT);//去除文字背景
    outtextxy(41, 118, "为已存在的两个地点");
    outtextxy(41, 157, "之间增加道路");
    outtextxy(31, 238, "道路端点1的ID：");
    outtextxy(31, 376, "道路端点2的ID：");
    outtextxy(31, 514, "道路名称：");

    Textbox tb1(31, 286, 314, 57, "", 5);
    tb1.settitle("请输入该道路端点1的ID：");
    tb1.show();
    Textbox tb2(31, 424, 314, 57, "", 5);
    tb2.settitle("请输入该道路端点2的ID：");
    tb2.show();
    Textbox tb3(31, 562, 314, 57, "", 5);
    tb3.settitle("请输入该道路名称：");
    tb3.show();

    //持续循环直到获取鼠标点击按钮的信号
    while (1) {
        if (Window::havemsg()) {
            m_msg = Window::getmsg();
            if (m_msg.x >= 0 && m_msg.x <= 68 && m_msg.y >= 0 && m_msg.y <= 83) {
                if (m_msg.message == WM_LBUTTONDOWN) { //点击返回键，回到主页功能骨架
                    return;
                }
            }

            btn1.setmsg(m_msg);
            if (btn1.isCliked()) { //点击了添加按钮
                string point1 = tb1.gettext();
                string point2 = tb2.gettext();
                string pathname = tb3.gettext();
                stringstream ss1(point1);
                stringstream ss2(point2);
                int id1 = 0; int id2 = 0;
                ss1 >> id1; ss2 >> id2; //取出两个端点的ID
                Dot* p1 = NULL; Dot* p2 = NULL;
                for (int i = 0; i < DotKu.size(); i++) {
                    if (p1 != NULL && p2 != NULL) { //两个地点都找到了，跳出
                        break;
                    }
                    else {
                        if (p1 == NULL && DotKu[i]->GetID() == id1) { //找到端点1
                            p1 = DotKu[i];
                        }
                        if (p2 == NULL && DotKu[i]->GetID() == id2) { //找到端点2
                            p2 = DotKu[i];
                        }
                    }
                }

                CreateStraight(p1, p2, pathname);
                //添加道路功能函数
                break; //添加完成则跳出循环
            }

            btn2.setmsg(m_msg);
            if (btn2.isCliked()) { //点击了返回按钮，回到主页功能骨架
                return;
            }

            tb1.setmsg(m_msg); //端点1输入
            if (tb1.isCliked()) {
                tb1.updatetext(0);
            }

            tb2.setmsg(m_msg); //端点2输入
            if (tb2.isCliked()) {
                tb2.updatetext(0);
            }

            tb3.setmsg(m_msg); //道路名称输入
            if (tb3.isCliked()) {
                tb3.updatetext(0);
            }
        }
    }
    //添加完成则跳出循环，并给出成功提示
    setbkcolor(RGB(255, 255, 255));
    cleardevice();
    //顶栏设置
    setfillcolor(RGB(130, 226, 255));
    fillroundrect(0, 0, 375, 83, 0, 0);
    settextcolor(RGB(0, 72, 125));
    settextstyle(36, 0, "黑体");//设置文本大小，垂直压缩，字体
    setbkmode(TRANSPARENT);//去除文字背景
    outtextxy(113, 20, "增加道路");

    //下栏图标设置
    IMAGE img;
    loadimage(&img, "./定位.png", 51, 50);
    putimage(160, 749, &img);
    setlinecolor(RGB(15, 19, 174));
    line(27, 774, 145, 774);
    line(230, 774, 348, 774);

    //按钮设置
    BasicButton btn(110, 545, 152, 64, 10, "确定");
    btn.setbtncl(96, 218, 255);
    btn.settext(0, 72, 125, 32, 153, 559);
    btn.show();

    IMAGE success;
    loadimage(&success, "./成功.png", 174, 174);
    putimage(101, 232, &success);
    settextcolor(RGB(0, 0, 0));
    settextstyle(36, 0, "黑体");//设置文本大小，垂直压缩，字体
    setbkmode(TRANSPARENT);//去除文字背景
    outtextxy(113, 431, "添加成功！");

    //持续循环直到获取鼠标点击按钮的信号
    while (1) {
        if (Window::havemsg()) {
            m_msg = Window::getmsg();
            btn.setmsg(m_msg);
            if (btn.isCliked()) { //点击了确定按钮，回到主页功能骨架
                return;
            }
        }
    }
}


//删除道路页面
void System::deletePath()
{
    setbkcolor(RGB(255, 255, 255));
    cleardevice();
    //顶栏设置
    setfillcolor(RGB(130, 226, 255));
    fillroundrect(0, 0, 375, 83, 0, 0);
    settextcolor(RGB(0, 72, 125));
    settextstyle(36, 0, "黑体");//设置文本大小，垂直压缩，字体
    setbkmode(TRANSPARENT);//去除文字背景
    outtextxy(113, 20, "删除道路");
    IMAGE retn;
    loadimage(&retn, "./返回.png", 16, 29);
    putimage(25, 27, &retn);

    //按钮设置
    BasicButton btn1(110, 407, 152, 64, 10, "删除");
    btn1.setbtncl(56, 164, 254);
    btn1.settext(255, 255, 255, 32, 154, 420);
    btn1.show();

    BasicButton btn2(110, 515, 152, 64, 10, "返回");
    btn2.setbtncl(217, 217, 217);
    btn2.settext(101, 101, 101, 32, 153, 528);
    btn2.show();

    //输入框设置
    settextcolor(RGB(0, 0, 0));
    settextstyle(32, 0, "黑体");//设置文本大小，垂直压缩，字体
    setbkmode(TRANSPARENT);//去除文字背景
    outtextxy(31, 222, "道路名称：");

    Textbox tb1(31, 281, 314, 57, "", 5);
    tb1.settitle("请输入待删除的道路名称：");
    tb1.show();

    //持续循环直到获取鼠标点击按钮的信号
    while (1) {
        if (Window::havemsg()) {
            m_msg = Window::getmsg();
            if (m_msg.x >= 0 && m_msg.x <= 68 && m_msg.y >= 0 && m_msg.y <= 83) {
                if (m_msg.message == WM_LBUTTONDOWN) { //点击返回键，回到主页功能骨架
                    return;
                }
            }

            btn1.setmsg(m_msg);
            if (btn1.isCliked()) { //点击了删除按钮
                string pathname = tb1.gettext();
                for (int i = 0; i < LineKu.size(); i++) {
                    if (pathname == LineKu[i]->Getname()) { //找到名字对应的道路
                        DeleteStraight(pathname); //调用删除道路函数
                    }
                }
                //删除道路功能函数
                break; //删除完成则跳出循环
            }

            btn2.setmsg(m_msg);
            if (btn2.isCliked()) { //点击了返回按钮，回到主页功能骨架
                return;
            }

            tb1.setmsg(m_msg); //道路名称输入
            if (tb1.isCliked()) {
                tb1.updatetext(0);
            }
        }
    }
    //添加完成则跳出循环，并给出成功提示
    setbkcolor(RGB(255, 255, 255));
    cleardevice();
    //顶栏设置
    setfillcolor(RGB(130, 226, 255));
    fillroundrect(0, 0, 375, 83, 0, 0);
    settextcolor(RGB(0, 72, 125));
    settextstyle(36, 0, "黑体");//设置文本大小，垂直压缩，字体
    setbkmode(TRANSPARENT);//去除文字背景
    outtextxy(113, 20, "删除道路");

    //下栏图标设置
    IMAGE img;
    loadimage(&img, "./定位.png", 51, 50);
    putimage(160, 749, &img);
    setlinecolor(RGB(15, 19, 174));
    line(27, 774, 145, 774);
    line(230, 774, 348, 774);

    //按钮设置
    BasicButton btn(110, 545, 152, 64, 10, "确定");
    btn.setbtncl(96, 218, 255);
    btn.settext(0, 72, 125, 32, 153, 559);
    btn.show();

    IMAGE success;
    loadimage(&success, "./成功.png", 174, 174);
    putimage(101, 232, &success);
    settextcolor(RGB(0, 0, 0));
    settextstyle(36, 0, "黑体");//设置文本大小，垂直压缩，字体
    setbkmode(TRANSPARENT);//去除文字背景
    outtextxy(113, 431, "删除成功！");

    //持续循环直到获取鼠标点击按钮的信号
    while (1) {
        if (Window::havemsg()) {
            m_msg = Window::getmsg();
            btn.setmsg(m_msg);
            if (btn.isCliked()) { //点击了确定按钮，回到主页功能骨架
                return;
            }
        }
    }
}